<?php
class Admin_Form_Order extends ZendX_JQuery_Form {
	protected $_title;
	protected $_type; // Create or Update
	protected $_submit;

	protected $_date;
	protected $_schedule;
	protected $_typeOrder;
	protected $_quantity;

	public $_user; // user id hidden

	public function __construct($type) {
		parent::__construct();
		$this->_type = $type;
		$this->create($this->_type);
	}

	// Create form base on type.
	protected function create($type) {
		$titleStr  = '';
		$submitStr = '';
		$url = new Zend_View_Helper_Url();
		$action = ' ';
		if ($type == 'create') {
			$action = $url->url(array('module'=>'admin', 'controller'=> 'order','action'=>'addorder'),'default');
			$submitStr = 'Create';
			$titleStr = 'Add schedule';
		} else if ($type == 'update'){
			$action = $url->url(array('module'=>'admin', 'controller'=> 'order','action'=>'updateorder'),'default');
			$submitStr = 'Update';
			$titleStr = 'Update schedule';
		}
		$this->setName($submitStr);
		$this->setAttrib('enctype', 'multipart/form-data');
		$this->setAction($action);

		//table

		$this->_title = new Zend_Form_Decorator_Captcha();
		$this->_title->render($titleStr);

		$this->_date = new ZendX_JQuery_Form_Element_DatePicker('date',
		array('jQueryParams' => array('dateFormat' => 'yy-mm-dd','showWeek'=>true))
		);
		$this->_date->setLabel('Date: ')
		->setRequired(true)
		->addValidator('NotEmpty');

		$this->_schedule = new Zend_Form_Element_Select('sid');
		$this->_schedule->setLabel('Schedule: ');

		$this->_typeOrder = new Zend_Form_Element_Select('type');
		$this->_typeOrder->setLabel('Type: ');


		$validateNumber = new Zend_Validate_Int();
		$validateBeween = new Zend_Validate_Between(array('min'=>1,'max' => 100));

		$this->_quantity = new Zend_Form_Element_Text('quantity');
		$this->_quantity->setLabel('Quantity: ')
		->setRequired(true)->addValidator($validateNumber)
		->addValidator($validateBeween);

		$this->_user = new Zend_Form_Element_Hidden('user');


		$this->_submit = new Zend_Form_Element_Submit('submit');
		$this->_submit->setLabel($submitStr);

		$this->addElements(array($this->_title,
		$this->_date,
		$this->_schedule,
		$this->_typeOrder,
		$this->_quantity,
		$this->_user,
		$this->_submit
		));
	}

	public function bindData($schedules, $types, Front_Model_User $user) {
		$data = array();
		foreach ($schedules as $schedule) {
			$value = 'From '.$schedule->_fromdate.' to '.$schedule->_todate;
			$data[$schedule->_sid] = $value;
		}
		$this->_schedule->setMultiOptions($data);
		if (isset($types)) {
			$typeData = array();
			foreach ($types as $type) {
				$typeData[$type->_tid] = $type->_typename;
			}
			$this->_typeOrder->setMultiOptions($typeData);
		}
		if (isset($user->_uid)) {
			$this->_user->setValue($user->_uid);
		}
	}

	public function populateData(User_Model_Order $orderInfo) {

		if (isset($orderInfo->_date)) {
			$this->_date->setValue($orderInfo->_date);
		}

		if (isset($orderInfo->_sid)) {
			$this->_schedule->setValue($orderInfo->_sid);
		}

		if (isset($orderInfo->_type)) {
			$this->_typeOrder->setValue($orderInfo->_type);
		}

		if (isset($orderInfo->_quantity)) {
			$this->_quantity->setValue($orderInfo->_quantity);
		}
	}

}